package com.sheng.sign.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sheng.sign.entity.Course;
import com.sheng.sign.entity.User;
import com.sheng.sign.service.UserService;

public class UserAction extends ActionSupport implements ModelDriven<User>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5626692290094748699L;
	protected String forwardView;//页面跳转
	protected static final String MAIN_JSP = "/main.jsp";
	protected static final String LIST_JSP = "/page/User_list.jsp";
	protected static final String ADD_JSP = "/page/User_add.jsp";
	protected static final String EDIT_JSP = "/page/User_edit.jsp";
	
	private UserService userService;
    private User user = new User();
    private List<User> lists ;
    
    
	public User getModel() {
		return user;
	}
	
	/**
	 * 
	 * @Description:打开注册页面
	 * @return
	 * String
	 * @exception:
	 * @author: huangshengjun 1532950421@qq.com
	 * @time:2019年12月7日 下午8:17:09
	 */
	public String openAdd(){
			setForwardView(ADD_JSP);
			return SUCCESS;
	}
	
	public String openEdit(){
		user = userService.findById(user);
			setForwardView(EDIT_JSP);
			return SUCCESS;
	}
	
	/**
	 * 
	 * @Description:注册
	 * @return
	 * String
	 * @exception:
	 * @author: huangshengjun 1532950421@qq.com
	 * @time:2019年12月7日 下午8:17:24
	 */
	public String register(){
		System.out.println("==来到了userAction=="+user);
		userService.register(user);
		return this.list();
	}
	
	public String login(){
		User u = null;
		u = userService.login(user);
		if(u!=null){
			//赋值给LoginUser，通过${LoginUser}显示用户名
			ActionContext.getContext().getSession().put("LoginUser", u.getUsername());
			setForwardView(MAIN_JSP);
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	public String list() {
		lists = userService.findAllList();
		System.out.println("Action的课程列表==="+lists);
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	
	public String edit() {
		System.out.println("==来到了editAction== "+user);
		boolean value = userService.edit(user);
		if(value){
			setForwardView(LIST_JSP);
			return this.list();
		}else{
			return ERROR;
		}
	}
	
	public String delete() {
		user = userService.findById(user);
		System.out.println("==来到了delAction== "+user);
		boolean value = userService.delete(user);
		if(value){
			setForwardView(LIST_JSP);
			return this.list();
		}else{
			return ERROR;
		}
	}
	
	public String findByUserId() {
		lists = userService.findByUserId(user);
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	
	public String getForwardView() {
		return forwardView;
	}
	public void setForwardView(String forwardView) {
		this.forwardView = forwardView;
	}
	
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<User> getLists() {
		return lists;
	}

	public void setLists(List<User> lists) {
		this.lists = lists;
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	
}
